// pages/post3/post3.js
const app = getApp(), core = require('../../utils/core'), util = require('../../utils/util'), WeValidator = require('../../utils/we-validator');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    job: {},
    benefits: []
  },

  check:function(e) {
    let benefits = this.data.benefits, index = e.currentTarget.dataset.index,
      benefitIds = this.data.job.benefit_ids, benefit = benefits[index], checked = !benefit.checked;
    let key = 'benefits['+index+'].checked';
    if(checked) {
      benefitIds.push(benefit.id);
      this.data.job.benefits.push(benefit);
    }else {
      let existIndex = benefitIds.indexOf(benefit.id);
      existIndex > -1 ? benefitIds.splice(existIndex, 1) : '';
      //去除job.benefits
      for(let i=0;i<this.data.job.benefits.length;i++) {
        if(this.data.job.benefits[i].id==benefit.id) {
          this.data.job.benefits.splice(i,1);
          break;
        }
      }
    }
    this.setData({
      [key]: checked,
      'job.benefit_ids': benefitIds,
      'job.benefits': this.data.job.benefits,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getOpenerEventChannel().on('jobTransfer', job => {
      job.benefit_ids = job.benefit_ids || [];
      job.benefits = job.benefits || [];
      util.getBenefits().then(benefits => {
        //将官方与job已选定的benefits合并，去重
        job.benefits.forEach(function (item) {
          for(let i=0;i<benefits.length;i++) {
            if(benefits[i].name===item.name) {
              //与官方重复
              return;
            }
          }
          //非官方，添加进benefits
          benefits.push(item);
        });
        benefits.forEach(item => {
          if(job.benefit_ids.indexOf(item.id)>-1) {
            item.checked = true;
          }
        });
        this.setData({benefits,job});
      });
    });
  },
  bindNextStep: function () {
    wx.navigateTo({url: '/pages/post5/post5', success: res => {
      res.eventChannel.emit("jobTransfer", this.data.job);
    }});
  },
  bindTagInput(event) {
    let tag = event.detail.value.replace(/^[\s,，]*/,'');
    if(tag!=='' && /[\s,，]$/.test(tag)) {
      tag = tag.replace(/[\s,，]*$/,'');
      if(tag.length>8) {
        wx.showToast({title: '标签最长8个字符',icon:'none'});
        return tag.substr(0,8);
      }
      //查找输入的标签是否在列表中
      for(let i=0;i<this.data.benefits.length;i++) {
        let item = this.data.benefits[i];
        if(item.name===tag) {
          //输入的标签已在列表中,如果未选中则直接选中，已选中不做任何事
          let index = this.data.job.benefit_ids.indexOf(item.id);
          if(index==-1) {
            this.check({currentTarget:{dataset:{index:i}}});
          }

          return "";
        }
      }
      core.post({url:'job_manage/get_benefit',data:{name:tag},success: ret=>{
        let benefit = ret.data.benefit;
        for(let i=0;i<this.data.benefits.length;i++) {
          let item = this.data.benefits[i];
          if(item.name===benefit.name) {
            //输入的标签已在列表中,如果未选中则直接选中，已选中不做任何事
            let index = this.data.job.benefit_ids.indexOf(item.id);
            if(index==-1) {
              this.check({currentTarget:{dataset:{index:i}}});
            }
            this.check({currentTarget:{dataset:{index:i}}});
            return;
          }
        }
        //不在列表中,添加进列表并选中
        benefit.checked = false;
        this.data.benefits.push(benefit);
        this.setData({benefits: this.data.benefits});
        this.check({currentTarget:{dataset:{index:this.data.benefits.length-1}}});

      }});
      return "";
    }
  },
});
